package org.bonorsoft.modules.control.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.wechat.entity.WxRequest;
import org.bonorsoft.modules.wechat.service.IEchoPhotoService;
import org.bonorsoft.modules.wechat.service.IWxRequestService;
import org.bonorsoft.modules.wechat.service.IWxResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@Controller
@RequestMapping("/control/request")
public class RequestController extends BonorController<WxRequest, IWxRequestService> {
    private String prefix = "control/request";

    @Autowired
    private IWxRequestService wxRequestService;
    @Autowired
    private IWxResponseService wxResponseService;
    @Autowired
    private IEchoPhotoService echoPhotoService;

    /**
     * 呼应 请求 首页
     */
    @RequiresPermissions("control:request:view")
    @GetMapping()
    public String index() {
        return prefix + "/request";
    }

    /**
     * 呼应 请求 分页列表查询
     *
     * @param request
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "呼应-分页列表查询")
    @RequiresPermissions("control:request:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(WxRequest request,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<WxRequest> queryWrapper = QueryGenerator.initQueryWrapper(request, req.getParameterMap());
        Page<WxRequest> page = new Page<WxRequest>(pageNo, pageSize);
        IPage<WxRequest> pageList = wxRequestService.paging(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 呼应 请求 添加页面
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 呼应 请求 添加
     *
     * @param request
     * @return
     */
    @AutoLog(value = "呼应-添加")
    @RequiresPermissions("control:request:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<?> add(@Validated WxRequest request, String photo) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String qqid = UUID.randomUUID().toString().replaceAll("-", "");
        request.setQqid(qqid).setTjrid(user.getUsername()).setTjrxm(user.getRealname()).setTjsj(new Date());
        wxRequestService.save(request);
        if (oConvertUtils.isNotEmpty(photo)) {
            String[] split;
            if (photo.contains(",")) {
                split = photo.split(",");
            } else {
                split = new String[]{photo};
            }
            for (String s : split) {
                echoPhotoService.addPhoto(qqid, s);
            }
        }
        return Result.ok("添加成功！");
    }

    /**
     * 呼应 详细
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap map) {
        map.put("request", wxRequestService.getById(id));
        map.put("photo", echoPhotoService.getPhoto(id));
        map.put("response", wxResponseService.getWxResponseByQqid(id));
        return prefix + "/detail";
    }

    /**
     * 三维码 呼应 详细
     */
    @GetMapping("/mobileDetail/{id}")
    public String requestDetail(@PathVariable("id") String id, ModelMap map) {
        map.put("request", wxRequestService.getById(id));
        map.put("photo", echoPhotoService.getPhoto(id));
        return "errorDetail/clapDetails";
    }

    /**
     * 三维码 呼应 列表
     */
    @GetMapping("/mobileList")
    public String requestList(ModelMap map) {
        List<WxRequest> request = wxRequestService.list();
        map.put("request", request);
        return "errorDetail/clapMore";
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "呼应-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        wxRequestService.removeById(id);
        echoPhotoService.deletePhoto(id);
        wxResponseService.removeByQqid(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "呼应-批量删除")
    @RequiresPermissions("control:request:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    @ResponseBody
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        wxRequestService.removeByIds(list);
        echoPhotoService.deletePhotoByIds(list);
        for (String qqid : list) {
            wxResponseService.removeByQqid(qqid);
        }
        return Result.ok("删除成功!");
    }
}
